package com.sx.gongzhong.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.gongzhong.R;
import com.sx.gongzhong.activity.ExposureDetailActivity;
import com.sx.gongzhong.adapter.ExposureListAdapter;
import com.sx.gongzhong.model.ExposureBean;
import com.sx.gongzhong.model.PublicBean;
import com.sx.gongzhong.pest.DeletePest;
import com.sx.gongzhong.pest.ExposureListPest;
import com.sx.gongzhong.utils.AppConstants;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.IExposureListView;
import com.sx.gongzhong.view.IVoidView;
import com.sx.gongzhong.widget.CustomDialog;
import com.sx.gongzhong.widget.LoadDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by XZY on 2017/12/28.
 * 爆料的fragment
 */
public class ExposureAllFragment extends LazyFragment implements SwipeRefreshLayout.OnRefreshListener
            ,IExposureListView{

    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<ExposureBean> exposureBeanList=new ArrayList<>();
    private ExposureListAdapter exposureListAdapter;
    private PublicBean typeBean;
    private boolean isPrepared=false;
    private int currentPage=1;
    private ExposureListPest listPest;
    private boolean isMyAdd = false;//当前用户添加的爆料

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.refresh_list,container,false);
        ButterKnife.bind(this,view);
        listPest=new ExposureListPest(this);
        Bundle bundle=getArguments();
        if (bundle!=null){
            typeBean= (PublicBean) bundle.getSerializable("Type");
            isMyAdd = bundle.getBoolean("isMyAdd");
        }
        initView();
        isPrepared=true;
//        if (typeBean==null){
            lazyLoad();//全部第一次进入就加载
//        }
        return view;
    }

    private void initView() {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(this);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
        exposureListAdapter = new ExposureListAdapter(getActivity(),R.layout.item_exposure_list,exposureBeanList);
        exposureListAdapter.setEmptyView(View.inflate(getActivity(),R.layout.load_error_view,null));
        recyclerView.setAdapter(exposureListAdapter);
        exposureListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage++;
                loadData();
            }
        },recyclerView);
        exposureListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(getActivity(), ExposureDetailActivity.class)
                .putExtra("ExposureID",exposureBeanList.get(position).getId()));
            }
        });
        if (isMyAdd){//我的爆料删除
            exposureListAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(final BaseQuickAdapter adapter, View view, final int position) {
                    CustomDialog.Builder builder = new CustomDialog.Builder(getActivity());
                    CustomDialog dialog = builder.setMessage("确定删除吗？").
                            setBackButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).setConfirmButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            LoadDialog.show(getActivity(),"删除中...");
                            new DeletePest(new IVoidView() {
                                @Override
                                public void voidView(String result) {
                                    LoadDialog.dismiss(getActivity());
                                    exposureBeanList.remove(position);
                                    exposureListAdapter.notifyDataSetChanged();
                                    ToastUtils.showToast(getActivity(),result);
                                }
                                @Override
                                public void showError(String message) {
                                    LoadDialog.dismiss(getActivity());
                                    ToastUtils.showToast(getActivity(),message);
                                }
                            }).deleteMyAdd("/api/columnManage/brokeNews/delete",exposureBeanList.get(position).getId());
                        }
                    }).create();
                    dialog.show();
                    return true;
                }
            });
        }
    }

    @Override
    protected void lazyLoad() {
        if(!isPrepared || !isVisible) {
            return;
        }
//        if (exposureBeanList==null||exposureBeanList.size()<1||typeBean == null){
            onRefresh();
//        }
    }

    @Override
    public void onRefresh() {
        currentPage=1;
        loadData();
    }
    private void loadData() {
        //swipeRefreshLayout.setRefreshing(true);
        if (isMyAdd){
            listPest.loadMyAddExposureList(typeBean!=null?typeBean.getId():"","",currentPage,6);
        }else {
            listPest.loadExposureList(typeBean!=null?typeBean.getId():"","",currentPage,6);
        }
    }

    @Override
    public void onDestroyView() {
        isPrepared=false;
        super.onDestroyView();
    }

    @Override
    public void showError(String message) {
        swipeRefreshLayout.setRefreshing(false);
        ToastUtils.showToast(getActivity(),message);
    }

    @Override
    public void loadExposureList(List<ExposureBean> exposureBean,int total) {
        swipeRefreshLayout.setRefreshing(false);
        if (currentPage == 1){
            exposureBeanList.clear();
            exposureListAdapter.setNewData(exposureBeanList);
        }
        if (exposureBean != null && exposureBean.size() > 0) {
            exposureBeanList.addAll(exposureBean);
        }
        if (exposureBeanList.size() >= total){
            exposureListAdapter.loadMoreEnd();
        }else {
            exposureListAdapter.loadMoreComplete();
        }
        exposureListAdapter.notifyDataSetChanged();
    }
}
